# -*- coding: utf-8 -*-
"""
Created on Wed Dec 29 23:05:17 2021

@author: chibi
"""

from PIL import Image


pic = '2.jpg' #图片档

def readimg(im):
     
   
    img = Image.open(im)
    old_w ,old_h =  img.size
    dim = (128, 128)
  
    # resize image
    img.thumbnail(dim, Image.ANTIALIAS)
    #img.show()
    #img = img.convert("L")
    #img = img.convert("1")
    text =''

    new_w = img.size[0]
    
    new_h = img.size[1]
    

    text += "#define    IMAGE_HEIGHT   %d\n" % new_h
    text += "#define    IMAGE_WIDTH    %d\n" % new_w
    text +="uint16_t static const PROGMEM image_bmp[] = {\n"
    
    for y in range(0,new_h):
        for x in range(0,new_w):
            rgb565 = (( img.getpixel((x,y))[0] & 0xf8)<<8) + (( img.getpixel((x,y))[1] & 0xfc)<<3) + ( img.getpixel((x,y))[2]>>3)
                
            #rgb565 = (( img.getpixel((x,y))[0]>>3)<<11) + (( img.getpixel((x,y))[1]>>2)<<5) + ( img.getpixel((x,y))[2]>>3)
                    
            #print(rgb565)

            new_rgb565 = str(hex(rgb565))

            if (x == new_w-1 and y == new_h-1):
                text += "%s" % new_rgb565
            elif (x==new_w-1):
                text += "%s,\n" % new_rgb565
            
            else:
                text += "%s," % new_rgb565
                   
           
    text += "\n};"
    
    with open('img2.c' , 'w') as f:
        f.write(text)
  
readimg(pic)